package com.lingx.jt808.server.netty;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.AdaptiveRecvByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

@Component
public class JT808Server implements Runnable {

    private ChannelFuture future = null;
    private EventLoopGroup bossGroup;
    private EventLoopGroup workerGroup;
    @Resource
    private JT808ServerInitializer jt808ServerInitializer;

    public void startServer() throws Exception {
        int port = 7755;
        System.out.println("Start JT808Server TCP port:" + port);
        bossGroup = new NioEventLoopGroup();
        workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup);
            b.channel(NioServerSocketChannel.class);
            b.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(64, 2048, 65536));
            b.childHandler(jt808ServerInitializer);

            // 服务器绑定端口监听
            future = b.bind(port).sync();
            future.channel().closeFuture().sync();
            // 可以简写为
            /* b.bind(portNumber).sync().channel().closeFuture().sync(); */
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    @PreDestroy
    public void stop() {
        try {
            // 监听服务器关闭监听
            if (future != null)
                future.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

    }

    @Override
    public void run() {
        try {
            this.startServer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
